package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述：
 *  针对网站的展示图书在数据库中的查询，并将查询结果进行缓存
 * @Author: Zhanghf
 * @Date: 2021/6/22 18:56
 */
@Service
@CacheConfig(cacheManager = "BookManager",cacheNames = "books")
public class BookCacheService {
    @Autowired
    private BookMapper bookMapper;

    @Cacheable(key = "#root.methodName")
     //@Async 这个异步之后会有对集合的subList，报空指针
    public List<Book> indexGoodRec()
    {
        //用于首页精品推荐
        List<Book> indexGoodRec = bookMapper.selectList(new QueryWrapper<Book>()
                .select("id","name","type","author")
                .orderByDesc("collections").last("limit 13"));
        return indexGoodRec;
    }
   // @Async
    @Cacheable(key = "#a0+#root.methodName")
    public List<Book> hitsRank(String type)
    {
        //根据点击量对不同类型的小说进行排序推荐，用于首页类型小说推荐、type页小说推荐、点击排行
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()
                .eq("type", type)
                .select("id","name","type","author","introduction")
                .orderByDesc("hits").last("limit 10"));
        return list;
    }
    @Cacheable(key = "#root.methodName")
    public List<Book> hitsRankOver()
    {
        //点击排行中的完本小说
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()
                .eq("state", "完本")
                .orderByDesc("hits").last("limit 10"));
        return list;
    }
    //@Async
    @Cacheable(key = "#root.methodName")
    public List<Book> indexLatestUpdate()
    {
        //用于首页最近更新小说列表
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()
                .select("id","name","type","author","latestnum","latesthref","latestdate","latestchapter")
                .orderByDesc("latestdate").last("limit 40"));
        return list;
    }
     //@Async
    @Cacheable(key = "#root.methodName")
    public List<Book> indexLatestOver()
    {
        //用于首页最近完本小说列表
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()
                .select("id","name","type","author")
                .eq("state","完本").orderByDesc("latestdate")
                .last("limit 40"));
        return list;
    }
    @Cacheable(key = "#a0+#root.methodName")
    public List<Book> typeLatestUpdate(String type)
    {
        //用于type页最近更新小说列表
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()

                .eq("type", type)
                .orderByDesc("latestdate").last("limit 40"));
        return list;
    }
    @Cacheable(key = "#a0+#root.methodName")
    public List<Book> typeLatestOver(String type)
    {
        //用于type页最近完本小说列表
        List<Book> list = bookMapper.selectList(new QueryWrapper<Book>()
                .eq("state","完本")
                .eq("type", type)
                .orderByDesc("latestdate").last("limit 40"));
        return list;
    }
    @Cacheable(key = "#a0+#root.methodName")
    public List<Book> collectionsRank(String type)
    {
        //用于收藏排行榜
        List<Book> collectionsRec = bookMapper.selectList(new QueryWrapper<Book>()
                .eq("type",type)
                .orderByDesc("collections")
                .last("limit 10"));
        return collectionsRec;
    }
    @Cacheable(key = "#root.methodName")
    public List<Book> collectionsRankOver()
    {
        //用于收藏排行榜中的完本排行
        List<Book> collectionsRec = bookMapper.selectList(new QueryWrapper<Book>().eq("state","完本").orderByDesc("collections").last("limit 10"));
        return collectionsRec;
    }
}
